﻿Imports System.Xml
Imports System.IO

Public Class mainForm
    Private WithEvents _userClient As New TyranntOnlineUserService.TyranntUserServiceClient
    Private WithEvents _charClient As New TyranntOnlineCharacterService.TyranntCharacterServiceClient
    Private _token As String = ""
    Private _alias As String = ""
    Private _userGroup As Integer = -1
    Private _usersXML As XElement
    Private _charsXML As XElement
    Private _charHash As New Hashtable

    Public Sub SetDetails(ByVal token As String, ByVal userAlias As String, ByVal userGroup As Integer)
        _token = token
        _alias = userAlias
        _userGroup = userGroup
    End Sub

    Private Sub Logout()
        If _token <> "" Then
            _userClient.LogoutAsync(_token)
        Else
            MessageBox.Show("Not Logged In", "Debug Information - Trying to log out when not logged in")
        End If
    End Sub

    Private Sub _client_LogoutCompleted(ByVal sender As Object, ByVal e As TyranntOnlineUserService.LogoutCompletedEventArgs) Handles _userClient.LogoutCompleted
        If e.Result <> "" Then
            MessageBox.Show(e.Result, "Debug Information - Logout Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        _token = ""
        _userGroup = -1
        _alias = ""
        Me.Close()
    End Sub

    Private Sub mainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        logoutToolStripButton.Enabled = True
        GetUsers()
        GetCharacters()
    End Sub

    Private Sub _client_GetUsersCompleted(ByVal sender As Object, ByVal e As TyranntOnlineUserService.GetUsersCompletedEventArgs) Handles _userClient.GetUsersCompleted
        Dim sr As New StringReader(e.Result)
        _usersXML = XElement.Load(sr)
        SetupUserHeadings()
        Dim users = _usersXML...<user>
        For Each aUser In users
            Dim item As New ListViewItem
            Select Case _userGroup
                Case 1
                    item.Text = aUser.@id
                    item.SubItems.Add(aUser.<name>.Value)
                    item.SubItems.Add(aUser.<alias>.Value)
                    Dim userGroup As Integer = CInt(aUser.<group>.Value)
                    If userGroup = 1 Then
                        item.SubItems.Add("Admin")
                    Else
                        item.SubItems.Add("User")
                    End If
                    item.SubItems.Add(aUser.<email>.Value)
                Case 2
                    item.Text = aUser.@name
            End Select
            usersListView.Items.Add(item)
        Next
        statusToolStripLabel.Text = ""
    End Sub

    Private Sub GetUsers()
        statusToolStripLabel.Text = "Fetching Online Users"
        _userClient.GetUsersAsync(_token)
    End Sub

    Private Sub GetCharacters()
        charactersListView.Enabled = False
        _charClient.GetCharactersAsync(_token)
    End Sub

    Private Sub _charClient_GetCharactersCompleted(ByVal sender As Object, ByVal e As TyranntOnlineCharacterService.GetCharactersCompletedEventArgs) Handles _charClient.GetCharactersCompleted
        If e.Result.Contains("[#]") Then
            MessageBox.Show(e.Result, "Get Characters Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If
        charactersListView.Enabled = True
        Dim sr As New StringReader(e.Result)
        _charsXML = XElement.Load(sr)
        charactersListView.Items.Clear()
        _charHash.Clear()
        For Each aChar As XElement In _charsXML...<character>
            Dim item As New ListViewItem
            Dim id As Integer
            If Integer.TryParse(aChar.@id, id) = False Then
                MessageBox.Show(aChar.@id + " Is not a valid integer", "Integer From String Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If
            Dim name As String = aChar.<name>.Value
            Dim race As String = aChar.<race>.Value
            Dim aClass As String = aChar.<class>.Value
            item.Text = name
            item.SubItems.Add(race)
            item.SubItems.Add(aClass)
            _charHash.Add(name, id)
            charactersListView.Items.Add(item)
        Next
    End Sub

    Private Sub SetupUserHeadings()
        Dim heading As New ColumnHeader
        usersListView.Columns.Clear()
        usersListView.Items.Clear()
        heading = New ColumnHeader
        heading.Text = "Online Users"
        heading.Width = 100
        usersListView.Columns.Add(heading)
    End Sub

    Private Sub logoutToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles logoutToolStripButton.Click
        Logout()
        logoutToolStripButton.Enabled = False
    End Sub

    Private Sub newCharacterToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newCharacterToolStripButton.Click
        Dim newChar As New newCharacterForm
        newChar.Token = _token
        newChar.ShowDialog()
        GetCharacters()
    End Sub

    Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click
        Dim id As Integer = CInt(_charHash(charactersListView.SelectedItems(0).Text))
        charactersListView.Enabled = False
        _charClient.DeleteCharacterAsync(_token, id)
    End Sub

    Private Sub _charClient_DeleteCharacterCompleted(ByVal sender As Object, ByVal e As TyranntOnlineCharacterService.DeleteCharacterCompletedEventArgs) Handles _charClient.DeleteCharacterCompleted
        If e.Result <> "" Then
            MessageBox.Show(e.Result, "Delete Character Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If
        _charClient.GetCharactersAsync(_token)
    End Sub

    Private Sub charactersContextMenuStrip_Opening(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles charactersContextMenuStrip.Opening
        If charactersListView.SelectedItems.Count = 0 Then
            e.Cancel = True
        Else
            DeleteToolStripMenuItem.Text = "Delete " + charactersListView.SelectedItems(0).Text
        End If
    End Sub
End Class
